#include "_Image.h"

#include "_Luly.h"
//加载图片
void _Image::loadImage(std::string fileName, std::string imageId) {
  SDL_Surface* image = IMG_Load(fileName.c_str());
  if (image != NULL) {
    //当没有同名的图片id时插入
    if (image_surface_list.find(imageId) == image_surface_list.end()) {
      image_surface_list.insert(
          std::pair<std::string, SDL_Surface*>(imageId, image));
    } else {
      SDL_LogError(0, "已有同名图片Id：%s", imageId.c_str());
      // throw;
    }

  } else {
    SDL_LogError(0, "图片加载失败：%s", fileName.c_str());
    // throw;
  }
}

bool _Image::hasImage(std::string imageId) {
  if (image_surface_list.find(imageId) != image_surface_list.end()) {
    return true;
  } else {
    return false;
  }
}

//销毁一张图片
void _Image::destroyImageSurface(std::string imageId) {
  if (image_surface_list[imageId] != NULL &&
      image_surface_list.find(imageId) != image_surface_list.end()) {
    SDL_FreeSurface(image_surface_list[imageId]);
    image_surface_list.erase(imageId);  //列表中也要剔除
  } else {
    SDL_LogError(0,
                 "该id对应的图片Surface为空指针或不在列表中，图片销毁失败:%s",
                 imageId.c_str());
    // throw;
  }
}

//绘制imageId对应的图片
//只绘制一帧
//不旋转，不缩放
void _Image::draw(std::string imageId, int x, int y) {
  SDL_Surface* surface = image_surface_list[imageId];  //不要销毁
  SDL_Rect src = {0, 0, surface->w, surface->h};
  SDL_Rect dest = {x, y, surface->w, surface->h};
  SDL_Texture* texture = SDL_CreateTextureFromSurface(
      _Luly::getInstance().p_renderer, surface);  //需要销毁
  SDL_RenderCopy(_Luly::getInstance().p_renderer, texture, &src, &dest);
  SDL_DestroyTexture(texture);
}

//绘制imageId对应的图片
//只绘制一帧
//旋转或缩放
//翻转还没有设置
void _Image::draw(std::string imageId, int x, int y, double scale,
                  double angle) {
  SDL_Surface* surface = image_surface_list[imageId];  //不要销毁
  SDL_Rect src = {0, 0, surface->w, surface->h};
  SDL_FRect dest = {float(x), float(y), float(surface->w * scale),
                    float(surface->h * scale)};
  SDL_Texture* texture = SDL_CreateTextureFromSurface(
      _Luly::getInstance().p_renderer, surface);  //需要销毁
  SDL_FPoint center = {0, 0};                     //中心点暂时设为0,0
  SDL_RenderCopyExF(_Luly::getInstance().p_renderer, texture, &src, &dest,
                    angle, &center, SDL_FLIP_NONE);
  SDL_DestroyTexture(texture);
}

void _Image::draw(std::string imageId, SDL_Rect cut, int x, int y,
                  double destWidth, double destHeight, double angle) {
  SDL_Surface* surface = image_surface_list[imageId];  //不要销毁
  SDL_FRect dest = {float(x), float(y), float(destWidth), float(destHeight)};
  SDL_Texture* texture = SDL_CreateTextureFromSurface(
      _Luly::getInstance().p_renderer, surface);  //需要销毁
  SDL_FPoint center = {0, 0};                     //中心点暂时设为0,0
  SDL_RenderCopyExF(_Luly::getInstance().p_renderer, texture, &cut, &dest,
                    angle, &center, SDL_FLIP_NONE);
  SDL_DestroyTexture(texture);
}

//获取imageId对应的SDL_Surface
SDL_Surface* _Image::getImageSurface(std::string imageId) {
  //如果imageId存在
  if (image_surface_list[imageId] != NULL &&
      image_surface_list.find(imageId) != image_surface_list.end()) {
    return image_surface_list[imageId];
  } else {
    SDL_LogError(
        0, "该id对应的图片Surface为空指针或不在列表中，surface获取失败:%s",
        imageId.c_str());
    // throw;
    return nullptr;
  }
}

_Image::~_Image() {
  for (std::pair<std::string, SDL_Surface*> i : image_surface_list) {
    SDL_FreeSurface(i.second);  //销毁所有图片
  }
}
